package io.sodabox.server.utils;

import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.json.JsonObject;


public final class DataUtils {

	public static final String[] SEPERATOR = {"^1^","^2^","^3^","^4^","^5^","^6^","^7^","^8^","^9^"};

	
	public static final String CMD_JOIN = "|JOIN|";
	

	public static final String CMD_TEST = "|TEST|";
	public static final String[] DATA_TEST_KEYS = {"TYPE","SOCK_ID","MESSAGE"};

	
	public static boolean join(Buffer data){
		return cmdStartWith(data, CMD_JOIN);
	}
	
	public static boolean test(Buffer data){
		return cmdStartWith(data, CMD_TEST);
	}
	
	public static JsonObject getTestData(Buffer data){
		String d = data.getString(CMD_TEST.length(), data.length());
		JsonObject obj = new JsonObject();
		
		System.out.println("DATA_TEST_KEYS.length"+DATA_TEST_KEYS.length);
		for(int i=0; i<DATA_TEST_KEYS.length; i++){
		
			if(i==0){
				obj.putString(DATA_TEST_KEYS[i], d.substring(0, d.indexOf(SEPERATOR[i])));
			}else if(i == (DATA_TEST_KEYS.length -1)){
				obj.putString(DATA_TEST_KEYS[i], d.substring(d.indexOf(SEPERATOR[i-1])+3, d.length()));
			}else{
				obj.putString(DATA_TEST_KEYS[i], d.substring(d.indexOf(SEPERATOR[i-1])+3, d.indexOf(SEPERATOR[i])));
			}
			
		}
		
		return obj;
	}
	
	
	private static boolean cmdStartWith(Buffer data, String command){
		if( command.equals(data.getString(0, command.length())) ){
			return true;
		}
		return false;
	}

	private DataUtils() {
		// utility
	}
}
